﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.TR.Library;
using ZX.TR.Bussiness;
using ZX.SYS.Library;

namespace  ZX.TR.Web
{
    public partial class QuestionnaireSubject : System.Web.UI.Page
    {
        BTrQuestionnaireSubject ts = new BTrQuestionnaireSubject();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                QuestionnaireId = this.Request.QueryString["id"];
                this.hiddenId.Value = QuestionnaireId;
                this.hiddenIts.Value = this.Request.QueryString["its"];
                if (QuestionnaireId != "")
                {
                    V_TrQuestionnaire tq = ts.GetAllQuestionnaireInfoById(new Guid(QuestionnaireId));
                    this.txtName.Text = tq.QuestionnaireName;
                    this.txtType.Text = tq.QStyleName;
                }
                IList<DatPhraseDetail> typeList = ts.GetAllTypeInfo();
                if (typeList != null)
                {     
                    this.drpType.DataSource = typeList;
                    this.drpType.DataTextField = "PhraseText";
                    this.drpType.DataValueField = "PhraseID";
                    this.drpType.DataBind();
                }

                IList<DatPhraseDetail> angleList = ts.GetAllRoleInfo();
                if (typeList != null)
                {
                    this.drpAngle.DataSource = angleList;
                    this.drpAngle.DataTextField = "PhraseText";
                    this.drpAngle.DataValueField = "PhraseValue";
                    this.drpAngle.DataBind();
                }            
            }
            //this.lstBoxSubject.Attributes["onDblClick"] = "change();";
        }
        public string QuestionnaireId
        {
            get;
            set;
        }

        #region 生成候选题目列表
        /// <summary>
        /// 生成候选题目列表
        /// </summary>
        private void BindlstBoxSubject()
        {
            string strType = this.drpType.SelectedValue;
            string angle = this.drpAngle.SelectedValue;
            string keywords = this.keywords.Text.Trim();
            string condcondition = " 1=1 ";
            //题目类型
            if (strType != "00")
            {
                condcondition += string.Format(" and SubjectType='{0}' ", new Guid(strType));
            }
            //题目角度
            if (angle != "00")
            {
                condcondition += string.Format(" and SubjectAngle='{0}' ", angle);
            }
            //题目关键字
            if (keywords != "")
            {
                keywords = keywords.Replace("'", "").Replace("-", "").Replace("*", "%").Replace("？","?").Replace("?", "_");
                if (keywords != "")
                {
                    condcondition += string.Format(" and (SubjectName like '{0}%' or SubjectContent like '{1}%') ", keywords, keywords);
                }
            }
            if (condcondition == " 1=1 ")
            {
                condcondition = " 1<>1";
            }
            IList<V_TrSubjectInfo> listSubjectInfo = ts.GetSubjectInfoToName(condcondition);
            this.lstBoxSubject.Items.Clear();
            if (listSubjectInfo != null)
            {
                for (int i = 0; i < listSubjectInfo.Count; i++)
                {
                    ListItem item = new ListItem("(" + listSubjectInfo[i].SubjectName + ")" + listSubjectInfo[i].SubjectContent, listSubjectInfo[i].TrSubjectInfoID.ToString());
                    item.Attributes.Add("title", listSubjectInfo[i].SubjectContent);
                    this.lstBoxSubject.Items.Add(item);
                }
            }
            else
            {
                this.lstBoxSubject.Items.Clear();
            }
        }

        protected void drpType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindlstBoxSubject();
        }

        protected void drpAngle_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindlstBoxSubject();
        }

        protected void keywords_Changed(object sender, EventArgs e)
        {
            BindlstBoxSubject();
        }
        #endregion

        protected void btnMoveOne_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.lstBoxSubject.Items.Count; i++)
            {
                if (this.lstBoxSubject.Items[i].Selected)
                {
                    if (this.lstBoxResult.Items.Count > 0)
                    {
                        int k = 0;
                        for (int j = 0; j < this.lstBoxResult.Items.Count; j++)
                        {
                            if (this.lstBoxSubject.Items[i].Text.ToString() == this.lstBoxResult.Items[j].Text.ToString())
                            {
                                k++;
                            }
                        }
                        if (k == 0)
                        {
                            this.lstBoxResult.Items.Add(new ListItem(this.lstBoxSubject.Items[i].Text, this.lstBoxSubject.Items[i].Value));
                        }
                    }
                    else
                    {
                        this.lstBoxResult.Items.Add(new ListItem(this.lstBoxSubject.Items[i].Text, this.lstBoxSubject.Items[i].Value));
                    }
                }
            }
        }
        protected void btnClearOne_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.lstBoxResult.Items.Count; i++)
            {
                if (this.lstBoxResult.Items[i].Selected)
                {
                    this.lstBoxResult.Items.RemoveAt(this.lstBoxResult.SelectedIndex);
                }
            }
        }
        protected void btnMoveAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.lstBoxSubject.Items.Count; i++)
            {
                if (this.lstBoxResult.Items.Count > 0)
                {
                    int k = 0;
                    for (int j = 0; j < this.lstBoxResult.Items.Count; j++)
                    {
                        if (this.lstBoxSubject.Items[i].Text.ToString() == this.lstBoxResult.Items[j].Text.ToString())
                        {
                            k++;
                        }
                    }
                    if (k == 0)
                    {
                        this.lstBoxResult.Items.Add(new ListItem(this.lstBoxSubject.Items[i].Text, this.lstBoxSubject.Items[i].Value));
                    }
                }
                else
                {
                    this.lstBoxResult.Items.Add(new ListItem(this.lstBoxSubject.Items[i].Text, this.lstBoxSubject.Items[i].Value));
                }
            }
        }
        protected void btnClearAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.lstBoxResult.Items.Count; i++)
            {
                this.lstBoxResult.Items.Clear();
            }
        }
    }
}